<?php

namespace App\Store\Controllers\Common;

use App\Common\Services\Date\TimerRange;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\Account;
use App\Store\Models\Basic\PaytypeList;
use App\Store\Models\Goods\GoodsAllot;
use App\Store\Models\Goods\GoodsReturn;
use App\Store\Models\Goods\GoodsStockin;
use App\Store\Models\Member\Member;
use App\Store\Models\Order\OrderDeposit;
use App\Store\Models\Order\OrderMaintenance;
use App\Store\Models\Order\OrderRecharge;
use App\Store\Models\Order\OrderRecovery;
use App\Store\Models\Order\OrderRecoveryGoods;
use App\Store\Models\Order\OrderSales;
use App\Store\Models\Order\OrderSalesGoods;
use App\Store\Models\Order\OrderSalesRecoveryGoods;
use Illuminate\Support\Facades\Log;

class DataController extends Controller
{


    public function getdatacount(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);

        $list1 = OrderSales::where('cp_id', $cp_id)->where('is_del', 1)->where('audit_status', 1)->whereIn('is_maker', [0, 2]);
        $list2 = OrderRecovery::where('cp_id', $cp_id)->where('is_del', 1)->where('audit_status', 1);
        $list3 = OrderDeposit::where('cp_id', $cp_id)->where('is_del', 1)->where('audit_status', 1);
        $list4 = OrderMaintenance::where('cp_id', $cp_id)->where('is_del', 1)->where('audit_status', 1);
        $list5 = OrderRecharge::where('cp_id', $cp_id)->where('is_del', 1)->where('audit_status', 1);


        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if ($store_ids) {//权限控制
            if (is_array($store_ids)) {//获取当前权限下的门店
                $list1->whereIn('store_id', $store_ids);
                $list2->whereIn('store_id', $store_ids);
                $list3->whereIn('store_id', $store_ids);
                $list4->whereIn('store_id', $store_ids);
                $list5->whereIn('store_id', $store_ids);
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) {//获取当前门店下的列表
                $list1->where('store_id', intval($store_ids));
                $list2->where('store_id', intval($store_ids));
                $list3->where('store_id', intval($store_ids));
                $list4->where('store_id', intval($store_ids));
                $list5->where('store_id', intval($store_ids));
            }
        }
        $ordercount = $list1->count() + $list2->count() + $list3->count() + $list4->count() + $list5->count();

        $goodsstockincount = GoodsStockin::where('cp_id', $cp_id)->whereIn('audit_status', [1, 2])
            ->where(function ($q) use ($store_ids) {
                if ($store_ids) {//权限控制
                    if (is_array($store_ids)) {//获取当前权限下的门店
                        $q->whereIn('store_id', $store_ids);
                    }
                    if (is_numeric($store_ids) && intval($store_ids) > 0) {//获取当前门店下的列表
                        $q->where('store_id', intval($store_ids));
                    }
                }
            })
            ->count();


        $goodsallotcount = GoodsAllot::where('cp_id', $cp_id)->where('is_del', 1)->whereIn('audit_status', [1, 2])
            ->where(function ($q) use ($store_ids) {
                if ($store_ids) {//权限控制
                    if (is_array($store_ids)) {//获取当前权限下的门店
                        $q->whereIn('start_store_id', $store_ids)->orWhereIn('target_store_id', $store_ids);
                    }
                    if (is_numeric($store_ids) && intval($store_ids) > 0) {//获取当前门店下的列表
                        $q->where('start_store_id', intval($store_ids))->orwhere('target_store_id', intval($store_ids));
                    }
                }
            })
            ->count();

        $goodsreturncount = GoodsReturn::where('cp_id', $cp_id)->where('is_del', 1)->whereIn('audit_status', [1, 2])
            ->where(function ($q) use ($store_ids) {
                if ($store_ids) {//权限控制
                    if (is_array($store_ids)) {//获取当前权限下的门店
                        $q->whereIn('store_id', $store_ids);
                    }
                    if (is_numeric($store_ids) && intval($store_ids) > 0) {//获取当前门店下的列表
                        $q->where('store_id', intval($store_ids));
                    }
                }
            })
            ->count();

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['ordercount' => $ordercount, 'goodsstockincount' => $goodsstockincount, 'goodsallotcount' => $goodsallotcount, 'goodsreturncount' => $goodsreturncount]]);
    }

    public function getpaydata(Request $request)
    {

        $store_id = intval($request->store_id);
        $where = [];
        $store_ids=[];
        if ($store_id > 0) {
            $where[] = ['store_id', $store_id];
        } else {
            $store_ids = StoreCommon::getCurrentAccountStoreId($request);
            $store_ids = (is_array($store_ids) && !empty($store_ids)) ? $store_ids : (intval($store_ids) > 0 ? [$store_ids] : []);
        }

        $where[] = ['create_at', '>=', strtotime(date('Y-m-d') . '00:00:00')];
        $where[] = ['create_at', '<=', strtotime(date('Y-m-d') . '23:59:59')];
        $where[] = ['audit_status', '!=', 3];


        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $paytype = PaytypeList::where(['cp_id' => $cp_id, 'is_del' => 1])->get()->toArray();


        // 开单
        $orderGoods = OrderSales::query()->where(['cp_id' => $cp_id, 'red_status' => 1, 'is_del' => 1])
            ->where($where)
            ->when(!empty($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->get(['real_money', 'pay_type_id', 'mixed_payment'])
            ->toArray();

        // 定金开单
        $deposit = OrderDeposit::query()->where(['cp_id' => $cp_id, 'is_del' => 1])
            ->where($where)
            ->when(!empty($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->selectRaw('money as real_money,pay_type_id,mixed_payment')
            ->get()
            ->toArray();

        // 充值开单
        $recharge = OrderRecharge::query()->where(['cp_id' => $cp_id, 'is_del' => 1])
            ->where($where)
            ->when(!empty($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->selectRaw('money as real_money,pay_type_id,mixed_payment')
            ->get()
            ->toArray();

        //  维修开单
        $maintenance = OrderMaintenance::query()->where(['cp_id' => $cp_id, 'is_del' => 1])
            ->where($where)
            ->when(!empty($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->selectRaw('money as real_money,pay_type_id,mixed_payment')
            ->get()
            ->toArray();


        // 查询纯回收金额
        $recovery = OrderRecovery::query()
            ->where(['cp_id' => $cp_id, 'is_del' => 1])
            ->where($where)
            ->selectRaw('total_money as real_money,pay_type_id,mixed_payment')
            ->get()
            ->toArray();

        // 纯回收处理为负数
        foreach ($recovery as &$item){
            $item['real_money'] =-abs($item['real_money']);
            if(!empty($item['mixed_payment'])){
                foreach ($item['mixed_payment']as &$pay){
                    $pay =-abs($pay);
                }
            }
        }

        $orderGoods = array_values(array_merge($orderGoods, $deposit, $recharge, $maintenance,$recovery));

        // 充值开单
        $sum_recharge_money = array_sum(array_column($recharge, 'real_money'));
        // 减去纯回收金额
        $sum_real_total_money_with_balance = array_sum(array_column($orderGoods, 'real_money'));

        $payMoney = [];
        foreach ($orderGoods as $goods) {
            if (empty($goods['mixed_payment'])) {
                $payMoney[$goods['pay_type_id']][] = $goods['real_money'] ?? 0;
            } else {
                foreach ($goods['mixed_payment'] as $key => $money) {
                    $ks = explode('_', $key)[1] ?? 0;
                    if ($ks > 0&&$money!=0) {
                        $payMoney[$ks][] = $money;
                    }
                }
            }
        }

        foreach ($paytype as &$type) {
            $type['total_money'] = empty($payMoney[$type['id']]) ? 0 : array_sum($payMoney[$type['id']]);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['paytype' => $paytype, 'sum_real_total_money_with_balance' => $sum_real_total_money_with_balance, 'sum_recharge_money' => $sum_recharge_money]]);


    }

    public function getsolarbdateccount(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $date = date('-m-d');
        $count = Member::where('cp_id', $cp_id)->where('solar_bdate', 'like', '%' . $date)->count();

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['count' => $count]]);
    }


    public function getmembercountdata(Request $request)
    {
        $day_start = strtotime(date('Y-m-d') . '00:00:00');
        $day_end = strtotime(date('Y-m-d') . '23:59:59');

        $month_start = strtotime(date('Y-m-01') . '00:00:00');
        $month_end = strtotime(date('Y-m-d') . '23:59:59');
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        $where = [];
        if ($start_time > 0 && $end_time > $start_time) {
            $end_time = $end_time + 86400;
            $where[] = ['create_at', '>=', $start_time];
            $where[] = ['create_at', '<=', $end_time];
        }
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);

        $totalcount = Member::where('cp_id', $cp_id)->where($where)->where('is_del', 1)->count();

        $daycount = Member::where('cp_id', $cp_id)->where('is_old', 1)->where('is_del', 1)->where('create_at', '>=', $day_start)->where('create_at', '<=', $day_end)->count();
        $monthcount = Member::where('cp_id', $cp_id)->where('is_old', 1)->where('is_del', 1)->where('create_at', '>=', $month_start)->where('create_at', '<=', $month_end)->count();
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['totalcount' => $totalcount, 'daycount' => $daycount, 'monthcount' => $monthcount]]);

    }

    public function getmemberdatashow(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $weektime_res = TimerRange::getThisWeek();
        $monthtime_res = TimerRange::getThisMonth();
        $day_res = ['start_time' => strtotime(date('Y-m-d') . '00:00:00'), 'end_time' => strtotime(date('Y-m-d') . '23:59:59')];
        //今日
        $data = [];
        $data['today']['totalcount'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->count();
        $data['today']['totalpoints'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->sum('points_current');
        $data['today']['totalshoppingtimes'] = OrderSales::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->count()
            + OrderRecharge::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->count() + OrderRecovery::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->count()
            + OrderDeposit::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->count() + OrderMaintenance::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->count();
        $data['today']['totalshoppingmoney'] = OrderSales::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->sum('pay_money')
            + OrderRecharge::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->sum('money') + OrderRecovery::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->sum('total_money')
            + OrderDeposit::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->sum('pay_money') + OrderMaintenance::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->sum('pay_money');

        $Member_tablename = (new Member())->getTable();
        $OrderSales_tablename = (new OrderSales())->getTable();
        $OrderRecharge_tablename = (new OrderRecharge())->getTable();
        $OrderRecovery_tablename = (new OrderRecovery())->getTable();
        $OrderDeposit_tablename = (new OrderDeposit())->getTable();
        $OrderMaintenance_tablename = (new OrderMaintenance())->getTable();


        $data['today']['newmem_totalpoints'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->where('is_old', 1)->whereBetween('create_at', [$day_res['start_time'], $day_res['end_time']])->sum('points_current');

        $data['today']['newmem_totalshoppingtimes'] = OrderSales::leftJoin($Member_tablename, $OrderSales_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderSales_tablename . '.cp_id', $cp_id], [$OrderSales_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderSales_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->count()
            + OrderRecharge::leftJoin($Member_tablename, $OrderRecharge_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecharge_tablename . '.cp_id', $cp_id], [$OrderRecharge_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderRecharge_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->count()
            + OrderRecovery::leftJoin($Member_tablename, $OrderRecovery_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecovery_tablename . '.cp_id', $cp_id], [$OrderRecovery_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderRecovery_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->count()
            + OrderDeposit::leftJoin($Member_tablename, $OrderDeposit_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderDeposit_tablename . '.cp_id', $cp_id], [$OrderDeposit_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderDeposit_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->count()
            + OrderMaintenance::leftJoin($Member_tablename, $OrderMaintenance_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderMaintenance_tablename . '.cp_id', $cp_id], [$OrderMaintenance_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderMaintenance_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->count();

        $data['today']['newmem_totalshoppingmoney'] = OrderSales::leftJoin($Member_tablename, $OrderSales_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderSales_tablename . '.cp_id', $cp_id], [$OrderSales_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderSales_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->sum($OrderSales_tablename . '.pay_money')
            + OrderRecharge::leftJoin($Member_tablename, $OrderRecharge_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecharge_tablename . '.cp_id', $cp_id], [$OrderRecharge_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderRecharge_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->sum($OrderRecharge_tablename . '.money')
            + OrderRecovery::leftJoin($Member_tablename, $OrderRecovery_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecovery_tablename . '.cp_id', $cp_id], [$OrderRecovery_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderRecovery_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->sum($OrderRecovery_tablename . '.total_money')
            + OrderDeposit::leftJoin($Member_tablename, $OrderDeposit_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderDeposit_tablename . '.cp_id', $cp_id], [$OrderDeposit_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderDeposit_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->sum($OrderDeposit_tablename . '.pay_money')
            + OrderMaintenance::leftJoin($Member_tablename, $OrderMaintenance_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderMaintenance_tablename . '.cp_id', $cp_id], [$OrderMaintenance_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->whereBetween($OrderMaintenance_tablename . '.create_at', [$day_res['start_time'], $day_res['end_time']])->sum($OrderMaintenance_tablename . '.pay_money');


        $data['week']['totalcount'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count();
        $data['week']['totalpoints'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('points_current');
        $data['week']['totalshoppingtimes'] = OrderSales::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count()
            + OrderRecharge::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count() + OrderRecovery::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count()
            + OrderDeposit::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count() + OrderMaintenance::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count();
        $data['week']['totalshoppingmoney'] = OrderSales::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('pay_money')
            + OrderRecharge::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('money') + OrderRecovery::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('total_money')
            + OrderDeposit::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('pay_money') + OrderMaintenance::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('pay_money');


        $data['week']['newmem_totalpoints'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->where('is_old', 1)->whereBetween('create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum('points_current');

        $data['week']['newmem_totalshoppingtimes'] = OrderSales::leftJoin($Member_tablename, $OrderSales_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderSales_tablename . '.cp_id', $cp_id], [$OrderSales_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderSales_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count()
            + OrderRecharge::leftJoin($Member_tablename, $OrderRecharge_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecharge_tablename . '.cp_id', $cp_id], [$OrderRecharge_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderRecharge_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count()
            + OrderRecovery::leftJoin($Member_tablename, $OrderRecovery_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecovery_tablename . '.cp_id', $cp_id], [$OrderRecovery_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderRecovery_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count()
            + OrderDeposit::leftJoin($Member_tablename, $OrderDeposit_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderDeposit_tablename . '.cp_id', $cp_id], [$OrderDeposit_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderDeposit_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count()
            + OrderMaintenance::leftJoin($Member_tablename, $OrderMaintenance_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderMaintenance_tablename . '.cp_id', $cp_id], [$OrderMaintenance_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderMaintenance_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->count();

        $data['week']['newmem_totalshoppingmoney'] = OrderSales::leftJoin($Member_tablename, $OrderSales_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderSales_tablename . '.cp_id', $cp_id], [$OrderSales_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderSales_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum($OrderSales_tablename . '.pay_money')
            + OrderRecharge::leftJoin($Member_tablename, $OrderRecharge_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecharge_tablename . '.cp_id', $cp_id], [$OrderRecharge_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderRecharge_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum($OrderRecharge_tablename . '.money')
            + OrderRecovery::leftJoin($Member_tablename, $OrderRecovery_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecovery_tablename . '.cp_id', $cp_id], [$OrderRecovery_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderRecovery_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum($OrderRecovery_tablename . '.total_money')
            + OrderDeposit::leftJoin($Member_tablename, $OrderDeposit_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderDeposit_tablename . '.cp_id', $cp_id], [$OrderDeposit_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderDeposit_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum($OrderDeposit_tablename . '.pay_money')
            + OrderMaintenance::leftJoin($Member_tablename, $OrderMaintenance_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderMaintenance_tablename . '.cp_id', $cp_id], [$OrderMaintenance_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->whereBetween($OrderMaintenance_tablename . '.create_at', [$weektime_res['start_time'], $weektime_res['end_time']])->sum($OrderMaintenance_tablename . '.pay_money');


        $data['month']['totalcount'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count();
        $data['month']['totalpoints'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('points_current');
        $data['month']['totalshoppingtimes'] = OrderSales::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count()
            + OrderRecharge::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count() + OrderRecovery::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count()
            + OrderDeposit::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count() + OrderMaintenance::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count();
        $data['month']['totalshoppingmoney'] = OrderSales::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('pay_money')
            + OrderRecharge::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('money') + OrderRecovery::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('total_money')
            + OrderDeposit::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('pay_money') + OrderMaintenance::where([['cp_id', $cp_id], ['audit_status', 2]])->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('pay_money');


        $data['month']['newmem_totalpoints'] = Member::where('cp_id', $cp_id)->where('is_del', 1)->where('is_old', 1)->whereBetween('create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum('points_current');

        $data['month']['newmem_totalshoppingtimes'] = OrderSales::leftJoin($Member_tablename, $OrderSales_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderSales_tablename . '.cp_id', $cp_id], [$OrderSales_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderSales_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count()
            + OrderRecharge::leftJoin($Member_tablename, $OrderRecharge_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecharge_tablename . '.cp_id', $cp_id], [$OrderRecharge_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderRecharge_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count()
            + OrderRecovery::leftJoin($Member_tablename, $OrderRecovery_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecovery_tablename . '.cp_id', $cp_id], [$OrderRecovery_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderRecovery_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count()
            + OrderDeposit::leftJoin($Member_tablename, $OrderDeposit_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderDeposit_tablename . '.cp_id', $cp_id], [$OrderDeposit_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderDeposit_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count()
            + OrderMaintenance::leftJoin($Member_tablename, $OrderMaintenance_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderMaintenance_tablename . '.cp_id', $cp_id], [$OrderMaintenance_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderMaintenance_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->count();

        $data['month']['newmem_totalshoppingmoney'] = OrderSales::leftJoin($Member_tablename, $OrderSales_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderSales_tablename . '.cp_id', $cp_id], [$OrderSales_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderSales_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum($OrderSales_tablename . '.pay_money')
            + OrderRecharge::leftJoin($Member_tablename, $OrderRecharge_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecharge_tablename . '.cp_id', $cp_id], [$OrderRecharge_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderRecharge_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum($OrderRecharge_tablename . '.money')
            + OrderRecovery::leftJoin($Member_tablename, $OrderRecovery_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderRecovery_tablename . '.cp_id', $cp_id], [$OrderRecovery_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderRecovery_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum($OrderRecovery_tablename . '.total_money')
            + OrderDeposit::leftJoin($Member_tablename, $OrderDeposit_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderDeposit_tablename . '.cp_id', $cp_id], [$OrderDeposit_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderDeposit_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum($OrderDeposit_tablename . '.pay_money')
            + OrderMaintenance::leftJoin($Member_tablename, $OrderMaintenance_tablename . '.mem_id', '=', $Member_tablename . '.id')->where([[$OrderMaintenance_tablename . '.cp_id', $cp_id], [$OrderMaintenance_tablename . '.audit_status', 2], [$Member_tablename . '.is_old', 1]])->whereBetween($Member_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->whereBetween($OrderMaintenance_tablename . '.create_at', [$monthtime_res['start_time'], $monthtime_res['end_time']])->sum($OrderMaintenance_tablename . '.pay_money');

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $data]);
    }

    public function getperformancedatashow(Request $request)
    {
        $info = StoreCommon::getCurrentAccountinfo($request);
        $store_id = intval($request->store_id);
        if ($store_id <= 0) {
            return response()->json(['code' => '001', 'msg' => '请选择门店']);
        }

        $query = Account::where(['cp_id' => $info['cp_id'], 'store_ids' => $store_id])->whereIn('type', [1, 2]);
        $store_ids = $info['store_ids'];
        if ($store_ids) {//权限控制
            if (is_array($store_ids)) {//获取当前权限下的门店
                $query->whereIn('store_ids', $store_ids);
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) {//获取当前门店下的列表
                $query->where('store_ids', intval($store_ids));
            }
        }
        $list = $query->select(['id', 'real_name'])->get()->toArray();
        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);

        $where = [];
        $where1 = [];
        $where[] = ['store_id', $store_id];
        $where[] = ['cp_id', $info['cp_id']];
        $where1[] = ['store_id', $store_id];
        $where1[] = ['cp_id', $info['cp_id']];
        if ($start_time > 0 && $end_time > $start_time) {
            $where[] = ['audit_at', '>=', $start_time];
            $where[] = ['audit_at', '<=', $end_time];
            $where1[] = ['red_audit_at', '>=', $start_time];
            $where1[] = ['red_audit_at', '<=', $end_time];
        }


        $where1[] = ['red_audit_status', 2];
        $where[] = ['audit_status', 2];

        foreach ($list as &$val) {
            $val['su'] = OrderSalesGoods::where($where)->where('is_sujin', 1)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') +
                OrderSalesRecoveryGoods::where($where)->where('is_sujin', 1)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') +
                OrderRecoveryGoods::where($where)->where('is_sujin', 1)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') - OrderSalesGoods::where($where1)->where('is_sujin', 1)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance');

            $val['notsu'] = OrderSalesGoods::where($where)->where('is_sujin', 2)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') +
                OrderSalesRecoveryGoods::where($where)->where('is_sujin', 2)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') +
                OrderRecoveryGoods::where($where)->where('is_sujin', 2)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') -
                OrderSalesGoods::where($where1)->where('is_sujin', 2)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance');

            $val['total'] = OrderSalesGoods::where($where)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') +
                OrderSalesRecoveryGoods::where($where)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') +
                OrderRecoveryGoods::where($where)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance') -
                OrderSalesGoods::where($where1)->where(function ($q) use ($val) {
                    $q->where('shopping_account_id1', $val['id'])->orWhere('shopping_account_id2', $val['id']);
                })->sum('performance');
        }
        $list1 = $list;
        $list2 = $list;
        $list3 = $list;
        array_multisort(array_column($list1, 'total'), SORT_DESC, $list1);
        array_multisort(array_column($list2, 'su'), SORT_DESC, $list2);
        array_multisort(array_column($list3, 'notsu'), SORT_DESC, $list3);

        $totaldata = [
            'total' => array_sum(array_column($list, 'total')),
            'su' => array_sum(array_column($list, 'su')),
            'notsu' => array_sum(array_column($list, 'notsu')),
        ];

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['list1' => $list1, 'list2' => $list2, 'list3' => $list3, 'totaldata' => $totaldata]]);

    }


}
